using System;
using Bag;
using Build;
using Cysharp.Threading.Tasks;
using Entity.Player;
using GameTime;
using Map;
using MoonFramework.Scripts.Event;
using MoonFramework.Scripts.Mono;
using MoonFramework.Scripts.Resoure;
using MoonFramework.Scripts.Tool.Timer;
using MoonFramework.Scripts.View.UI;
using MoonFramework.Template;
using MoonFramework.Test;
using Science;
using Tool;
using Tool.Name;
using UI;
using UI.Bag;
using UI.Build;
using UnityEngine;
using InputManager = Tool.InputManager;

/// <summary>
/// 游戏场景管理器
/// </summary>
public class GameSceneManager : BaseManager<GameSceneManager>
{
    public bool isGameOver;
    
    public async UniTaskVoid StartGame()
    {
        UIManager.Instance.CloseAll();
        gameLoadWindow = UIManager.Instance.Show<UI_GameLoadWindow>(); //加载进度条
            
        //初始化角色,(相机可以用虚拟相机代替)
        var entityData = ArchiveManager.Instance.EntityData;
        GameObject playerInstance = await ResourceManager.Instance.LoadAsync<GameObject>("Player", (player) =>
        {
            player.name = entityData.Name;
        });
        PlayerViewController viewController = playerInstance.GetComponent<PlayerViewController>();
        viewController.Init();
        
        UIManager.Instance.Show<UI_MainInfoWindow>(); //初始化主界面
        BagManager.Instance.Init(); //初始化背包
        TimeManager.Instance.Init();    //初始化时间管理器
        //初始化地图
        MapManager.Instance.UpdateViewer(viewController.transform,entityData);
        MapManager.Instance.Init().Forget();
        InputManager.Instance.Init();   //初始化IO管理器 
        BuildManager.Instance.Init();   //初始化建造管理器
        ScienceManager.Instance.Init(); //初始化科技管理器
        Timer.Instance.Init();          //初始化定时器
        
        MonoManager.Instance.SendUpdateEvent(OnUpdate); //监控ESC暂停
    }

    // 关闭游戏场景 保存存档 进入菜单场景
    public void CloseAndSaveGame()
    {
        //存档
        EventCenter.Instance.Callback(EventName.SaveGame);
        //进入菜单场景
        EnterMenuScene();
    }
        
    public async UniTaskVoid EndGame()
    {
        isGameOver = true;
        // 删除存档
        ArchiveManager.Instance.ClearArchive();
        // 延迟进入进入新场景
        await UniTask.Delay(TimeSpan.FromSeconds(1));
        EnterMenuScene();
    }

    public void EnterMenuScene()
    {
        Time.timeScale = 1;
        //回收场景资源
        MapManager.Instance.OnCloseGameScene();
        EventCenter.Instance.Clear();
        UIManager.Instance.CloseAll();
        MonoManager.Instance.Clear();
        //进入新场景
        GameManger.Instance.EnterMenu();
    }

    public void OnApplicationQuit()
    {
        if (isGameOver)
        {
            //还处于游戏中，突发事故，紧急存档
            EventCenter.Instance.Callback(EventName.SaveGame);
        }
    }

    #region 监控ESC暂停
    private bool _isPause;
    
    private void OnUpdate()
    {
        if (!Input.GetKeyDown(KeyCode.Escape)) return;
        _isPause = !_isPause;
        if (_isPause) PauseGame();
        else UnPauseGame();
    }

    private void PauseGame()
    {
        _isPause = true;
        UIManager.Instance.Show<UI_PauseWindow>();
        Time.timeScale = 0;
    }

    public void UnPauseGame()
    {
        _isPause = false;
        UIManager.Instance.Close<UI_PauseWindow>();
        Time.timeScale = 1;
    }
    #endregion
    
    #region 加载进度
        
    private UI_GameLoadWindow gameLoadWindow;
        
    /// <summary>
    /// 更新进度
    /// </summary>
    /// <param name="cur"></param>
    /// <param name="max"></param>
    public void UpdateMapProgress(int cur, int max)
    {
        float curProgress = (100 / max) * cur;
        if (cur == max)
        {
            gameLoadWindow.UpdateProgress(100);
            gameLoadWindow.Close();
            gameLoadWindow = null;
        }
        else
        {
            gameLoadWindow.UpdateProgress(curProgress);
        }
    }
    #endregion
}